package Test.Exam;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @ClassName problem_8
 * @since: 2024/3/23 17:51
 * @auth: kirito
 * @description:
 * 美团笔试
 * 比较两个字符串s1,s2,将两个字符串的所有字母按如下方式可分为3类（3类字母数之和与s1，s2所有字母数之和相等）
 * 1.字母名相同且位置相同
 * 2.字母名相同但位置不同
 * 3.字母不相同
 * 请分别输出这3类的字母个数
 *
 * 例如,输入:
 * s1 = "abcdefc";
 * s2 = "abacafefh";
 * 输出结果: 6,8,2
 *
 * 	for map1:
 * 		char zimu = key
 * 		int count = value
 * 		if(map2.get(zimu)!=null){
 * 			int count_2 = abs(map2.get(zimu))
 * 			count-=count_2
 * 			map1.put(zimu,count)
 * 			map2.remove(zimu)
 *                }else{
 * 			add_字母不相同
 *        }
 *
 * 	for	map2
 * 		add_字母不相同
 **/
public class problem_9 {
    public static void main(String[] args) {
        String s1 = "abcdefc";
        String s2 = "abacafefh";
        countWord(s1, s2);
    }

    private static void countWord(String s1, String s2) {
        int len1 = s1.length();
        int len2 = s2.length();
        int count_1 = 0;
        int count_2 = 0;
        int count_3 = 0;
        Map<Character, Integer> map1 = new HashMap<>();
        Map<Character, Integer> map2 = new HashMap<>();
        Map<Character, Integer> map3 = new HashMap<>();

        if (len1 > len2) {
            for (int i = 0; i < len2; i++) {
                if (s1.charAt(i) == s2.charAt(i)) {
                    count_1 += 2;
                    map3.merge(s1.charAt(i), 1,Integer::sum);
                }
                map1.merge(s1.charAt(i), 1, Integer::sum);
                map2.merge(s2.charAt(i), 1, Integer::sum);
            }
            for (int i = len2; i < len1; i++) {
                map1.merge(s1.charAt(i), 1, Integer::sum);
            }
        }else{
            for (int i = 0; i < len1; i++) {
                if (s1.charAt(i) == s2.charAt(i)) {
                    count_1 += 2;
                    map3.merge(s1.charAt(i), 1,Integer::sum);
                }
                map1.merge(s1.charAt(i), 1, Integer::sum);
                map2.merge(s2.charAt(i), 1, Integer::sum);
            }
            for (int i = len1; i < len2; i++) {
                map2.merge(s2.charAt(i), 1, Integer::sum);
            }
        }

        for (Map.Entry<Character, Integer> map : map1.entrySet()) {
            Character key = map.getKey();
            Integer value_1 = map.getValue();
            if (map2.get(key) != null) {
                Integer value_2 = map2.get(key);
                Integer orDefault = map3.getOrDefault(key, 0);
                count_2 += value_1 + value_2 - 2 * orDefault;
                map2.remove(key);
            }else{
//                System.out.println(key + "   " + value_1);
                count_3 += value_1;
            }
        }
        for (Map.Entry<Character, Integer> map : map2.entrySet()) {
            Integer value = map.getValue();
//            System.out.println(map.getKey() + " " + value);
            count_3 += value;
        }
        System.out.println(count_1);
        System.out.println(count_2);
        System.out.println(count_3);

    }


}
